package com.zhz.thymeleafmp.service.impl;

import com.zhz.thymeleafmp.entity.User;
import com.zhz.thymeleafmp.mapper.DeptMapper;
import com.zhz.thymeleafmp.mapper.UserMapper;
import com.zhz.thymeleafmp.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public User login(String name, String password) {
        User user = userMapper.getUserByName(name);
        if (user != null && user.getPassword().equals(password)) {
            user.setDept(deptMapper.selectById(user.getDeptid()));
            return user;
        }
        return null;
    }

    @Override
    public List<User> getUserList() {
        return userMapper.selectList(null);
    }

    @Override
    public boolean addUser(User user) {
        int result =  userMapper.insert(user);
        boolean flag = false;
        if (result > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean modify(User user) {
        int result =  userMapper.updateById(user);
        boolean flag = false;
        if (result > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean delete(Long id) {
        int result =  userMapper.deleteById(id);
        boolean flag = false;
        if (result > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public User getUser(Long id) {
        return userMapper.selectById(id);
    }
}
